import 'package:fijkplayer/fijkplayer.dart';
import 'package:flutter/material.dart';
import 'package:huajianshe_app/base/BaseState.dart';
import 'package:huajianshe_app/widget/appbar/CAppBar.dart';

import 'VideoPlayerUI.dart';

class VideoPlayerWidget extends StatefulWidget {

  final String? url;

  const VideoPlayerWidget(this.url, {Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _VideoPlayerWidgetState();
  }
}

class _VideoPlayerWidgetState extends BaseState<VideoPlayerWidget> {

  final FijkPlayer player = FijkPlayer();
  bool _playing = false;

  @override
  void initState() {
    super.initState();
    print('video url: ${widget.url}');
    if (widget.url != null) player.setDataSource(widget.url!, autoPlay: true);
    player.addListener(() {
      FijkValue value = player.value;

      bool playing = (value.state == FijkState.started);
      if (playing != _playing) {
        _playing = playing;
        setState(() => null);
      }

    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body:
            Stack(children: [
              Container(
                alignment: Alignment.center,
                child: FijkView(
                    player: player,
                    color: Colors.black,
                    panelBuilder: (FijkPlayer player, FijkData data1, BuildContext context, Size viewSize, Rect texturePos) {
                      return VideoPlayerUI(player: player, buildContext: context, viewSize: viewSize, texturePos: texturePos, title: '');
                    }
                ),
              ),
              if (!_playing) CAppBar(backImgUrl: 'ic_white_arrow')
            ])
        );
  }

  @override
  void dispose() {
    super.dispose();
    player.release();
  }
}